using System;
using System.Collections;
using System.Threading;

namespace monoServer
{
	public class ThreadManager
	{
		private DataManager dm;
		private Hashtable _threads;
		private int maxThreads;
		private Queue threadQueue; 
		
		public ThreadManager (int mx, DataManager dm)
		{
			this.threadQueue = new Queue();
			this.dm = dm;
			this.maxThreads = mx;
			this._threads = new Hashtable();
		}
		
		public ulong getNewThread(ThreadStart method)
		{
			ulong tid = this.dm.curentThreadKey;
			this._threads.Add(tid, new Thread(method));
			return tid;
		}
		
		public void startThread(ulong key)
		{
			if(this._threads.Count < this.maxThreads)
			{
				((Thread)this._threads[key]).Start();
			} else {
				this.threadQueue.Enqueue(key);
			}
		}
		
		public void stopThread(ulong key)
		{
			Thread t = (Thread)this._threads[key];
			t.Abort();
			if(this.threadQueue.Count > 0)
			{
				ulong key2 = (ulong)this.threadQueue.Dequeue();
				((Thread)this._threads[key2]).Start();
			}
		}
	}
}

